/**
 * Operaciones de entrada y salida en el sistema de ficheros
 * 
 * @author GreaseMonkey (modificado por Javier Pérez)
 */
var RF_IO = {
	/**
	 * Obtiene un directorio para plugins
	 * - Primero intenta obtenerlo del directorio principal de Firefox
	 * - Si no lo consigue, obtiene un directorio dentro de "content" (en el directorio de la extensión)
	 * @author GreaseMonkey
	 */
	getPluginDir: function() {
		var dir = RF_IO.getNewPluginDir();
		if (dir.exists) return dir;
		
		// Crea el directorio "plugins" dentro de la extensión, en "content/"
		var file = RF_IO.getContentDir();
		file.append('plugins');
		return file;
	},
	
	/**
	 * Obtiene el directorio de plugins en el directorio raiz de Firefox
	 * @author GreaseMonkey
	 */
	getNewPluginDir: function() {
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
			.getService(Components.interfaces.nsIProperties)
			.get("ProfD", Components.interfaces.nsILocalFile);
		file.append('rainfox_plugins');
		return file;
	},
	
	/**
	 * Obtiene el directorio "content" de la extensión
	 * @author GreaseMonkey
	 */
	getContentDir: function() {
		var reg = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
			.getService(Components.interfaces.nsIChromeRegistry);
		var ioSvc = Components.classes["@mozilla.org/network/io-service;1"]
			.getService(Components.interfaces.nsIIOService);
		var proto = Components.classes["@mozilla.org/network/protocol;1?name=file"]
			.getService(Components.interfaces.nsIFileProtocolHandler);
		var chromeURL = ioSvc.newURI("chrome://rainfox/content", null, null);
		var fileURL = reg.convertChromeURL(chromeURL);
		var file = proto.getFileFromURLSpec(fileURL.spec).parent;
		return file;
	},

	/**
	 * Obtiene un apuntador de fichero de un plugin
	 * @param {Object} filename
	 */	
	getPluginFile: function(filename) {
		var file = RF_IO.getPluginDir();
		file.append(filename);
		return file;
	},

	/**
	 * Crea un fichero temporal
	 * 
	 * @author GreaseMonkey
	 */
	getTempFile: function() {
		var file = Components.classes["@mozilla.org/file/directory_service;1"]
			.getService(Components.interfaces.nsIProperties)
			.get("TmpD", Components.interfaces.nsILocalFile);
		file.append("rf_" + new Date().getTime());
		return file;
	},

	/**
	 * Abre un fichero para escritura
	 * 
	 * @author GreaseMonkey
	 * @param {String} file
	 */
	getWriteStream: function(file) {
		var stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
			.createInstance(Components.interfaces.nsIFileOutputStream);
		stream.init(file, 0x02 | 0x08 | 0x20, 420, -1);
		return stream;
	},
	
	/**
	 * Obtiene el contenido de una URL 
	 * @author GreaseMonkey
	 * @param {String} aURL
	 * @param {String} charset
	 */
	getContents: function(aURL, charset){
		if( !charset ) {
			charset = "UTF-8"
		}

		var ioService=Components.classes["@mozilla.org/network/io-service;1"]
			.getService(Components.interfaces.nsIIOService);
		var scriptableStream=Components
			.classes["@mozilla.org/scriptableinputstream;1"]
			.getService(Components.interfaces.nsIScriptableInputStream);

		// http://lxr.mozilla.org/mozilla/source/intl/uconv/idl/nsIScriptableUConv.idl
		var unicodeConverter = Components
			.classes["@mozilla.org/intl/scriptableunicodeconverter"]
			.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
		unicodeConverter.charset = charset;

		var channel=ioService.newChannelFromURI(aURL);
		var input=channel.open();
		scriptableStream.init(input);
		var str=scriptableStream.read(input.available());
		scriptableStream.close();
		input.close();

		try {
			return unicodeConverter.ConvertToUnicode(str);
		} catch( e ) {
			return str;
		}
	},
}